
%macro get_employees;

	proc printto new log = "&logdir./get_employees.txt";
	run;
	
		data orig;
			set rlib.f941(where=(ertc>0));
			keep firm_id;
		run;
		
		data appended;
			set orig(keep=firm_id) rlib.amendments(keep=firm_id);
		run;
		proc sort data = appended nodupkey;
			by firm_id;
		run;
		
			
		data _null_;
			set appended;
			file "&tmpdir./tinlist" dlm=',';
			put firm_id;
		run;



		/****************************************************
		*													*
		*					Main SQL call					*
		*													*
		****************************************************/
		proc sql;
		[redacted]
			(
				select
					a.[redacted] as person_id,
					a.[redacted] as firm_id,
					a.[redacted] as year,
					max(coalesce(a.[redacted],0) + coalesce(a.[redacted],0)) as wgs_firm
				from irmf_w2 as a
					inner join #tinlist as b
						on(a.[redacted] = b.firm_id)
				where a.[redacted] in(2020,2021)
					and a.[redacted] <= 4
					and coalesce(a.[redacted],0) + coalesce(a.[redacted],0) > 0
				group by a.[redacted],
					a.[redacted],
					a.[redacted]
				order by a.[redacted],
					a.[redacted],
					a.[redacted]
								
				

		);
	disconnect from iq;
	quit;
	
	
	/* take out the owners */
	proc sort data = rlib.ind_owners;
		by person_id firm_id;
	run;
	
	data rlib.own_w2;
		merge sample(in=a) rlib.ind_owners(keep=person_id firm_id in=b);
		by person_id firm_id;
		if a = 1 & b = 0;
	run;
	
	
	
	

	proc printto;
	run;
	
	
%mend;

